package com.ruoyi.web.controller.mall;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.agri.api.service.mall.IWxUserService;
import com.ruoyi.common.model.mall.dto.req.LoginReq;
import com.ruoyi.common.model.mall.dto.req.UserAddressReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author huixh
 */
@Api("小程序用户口")
@RestController
@RequestMapping("/wx/user")
public class WxUserController extends BaseController {

    @Resource
    private IWxUserService IWxUserService;

    @ApiOperation(value = "微信登录")
    @PostMapping("/login")
    public AjaxResult wxLogin(@RequestBody LoginReq loginReq) {
        return AjaxResult.success(IWxUserService.login(loginReq));
    }

    @GetMapping("/hasUser")
    public AjaxResult hasUser(@RequestParam("phone") String phone){
        return AjaxResult.success(IWxUserService.hasUserInfo(phone));
    }

    @ApiOperation(value = "获取用户地址列表")
    @GetMapping("/address")
    public AjaxResult getUserAddressList(@RequestHeader("Authorization") String authorizationHeader) {
        String token = getAuthorizationHeader(authorizationHeader);
        return AjaxResult.success(IWxUserService.getUserAddressList(token));
    }

    @ApiOperation(value = "添加用户地址")
    @PostMapping("/address")
    public AjaxResult addUserAddress(@RequestHeader("Authorization") String authorizationHeader, @RequestBody UserAddressReq addressReq) {
        String token = getAuthorizationHeader(authorizationHeader);
        IWxUserService.addUserAddress(addressReq, token);
        return success();
    }

    @ApiOperation(value = "删除用户地址")
    @DeleteMapping("/address/{id}")
    public AjaxResult deleteUserAddress(@RequestHeader("Authorization") String authorizationHeader, @PathVariable("id") Long id) {
        String token = getAuthorizationHeader(authorizationHeader);
        IWxUserService.deleteUserAddress(id, token);
        return success();
    }

    @ApiOperation(value = "获取用户地址")
    @GetMapping("/address/{id}")
    public AjaxResult getUserAddress(@RequestHeader("Authorization") String authorizationHeader, @PathVariable("id") Long id) {
        String token = getAuthorizationHeader(authorizationHeader);
        return AjaxResult.success(IWxUserService.getUserAddress(id, token));
    }

    @ApiOperation(value = "修改用户地址")
    @PutMapping("/address/{id}")
    public AjaxResult updateUserAddress(@RequestHeader("Authorization") String authorizationHeader, @RequestBody UserAddressReq addressReq, @PathVariable("id") Long id) {
        String token = getAuthorizationHeader(authorizationHeader);
        IWxUserService.updateUserAddress(id,addressReq, token);
        return success();
    }
}
